import taskOrderApi from '@/api/order/taskOrder'
import proofApi from '@/api/task/proof'
import permission from '@/directive/permission/index.js'

export default {
  //如果需要标签页缓存生效，则需要保证name值和菜单管理中的编码值一致
  name: 'taskOrder',
  directives: { permission },
  data() {
    return {
      formVisible: false,
      innerVisible: false,
      formTitle: '添加接单订单类',
      isAdd: true,
      form: {
        taskId:'',
        issuerId:'',
        state:'',
        taskTitle:'',
        submitTime:'',
        timeLimit:'',
        autoAuditTime:'',
        auditTime:'',
        autoWaiverTime:'',
        result:'',
        reason:'',
        appeal:'',
        failureRecovery:'',
        sysUserId:'',
        id: ''
      },
      failureRecovery:'',
      appealResult:0,
      taskPoofs:[],
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
        state: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {}
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  computed: {

    //表单验证
    rules() {
      return {
        // cfgName: [
        //   { required: true, message: this.$t('config.name') + this.$t('common.isRequired'), trigger: 'blur' },
        //   { min: 3, max: 2000, message: this.$t('config.name') + this.$t('config.lengthValidation'), trigger: 'blur' }
        // ]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
        taskOrderApi.getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.listQuery.state = ''
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    resetForm() {
      this.form = {
        taskId:'',
        issuerId:'',
        state:'',
        taskTitle:'',
        submitTime:'',
        timeLimit:'',
        autoAuditTime:'',
        auditTime:'',
        autoWaiverTime:'',
        result:'',
        reason:'',
        appeal:'',
        failureRecovery:'',
        sysUserId:'',
        id: ''
      }
    },handleAppeal(result){
      this.appealResult = result
      this.innerVisible = true
    },submitAppeal(orderId){
      if(this.appealResult == 0){
        this.$message({
          message: '请选择处理申述类型',
          type: 'warning'
        })
        this.innerVisible = false
        return false
      }
        const date = {
          orderId:orderId,
          appealResult:this.appealResult,
          failureRecovery:this.failureRecovery,
        }
      console.log(date);
      taskOrderApi.submitAppeal(date).then(res =>{
          if(res.code == 20000){
            this.innerVisible = false
            this.formVisible = false
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.search()
          }else {
            this.$message({
              message: res.msg,
              type: 'warning'
            })
          }

        })
    },
    add() {
      this.formTitle = '添加接单订单类'
      this.formVisible = true
      this.isAdd = true

      if(this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      //如果表单初始化有特殊处理需求,可以在resetForm中处理
          },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
            const formData = {
                id:this.form.id,
                taskId:this.form.taskId,
                issuerId:this.form.issuerId,
                state:this.form.state,
                taskTitle:this.form.taskTitle,
                submitTime:this.form.submitTime,
                timeLimit:this.form.timeLimit,
                autoAuditTime:this.form.autoAuditTime,
                auditTime:this.form.auditTime,
                autoWaiverTime:this.form.autoWaiverTime,
                result:this.form.result,
                reason:this.form.reason,
                appeal:this.form.appeal,
                failureRecovery:this.form.failureRecovery,
                sysUserId:this.form.sysUserId,
            }
            if(formData.id){
                taskOrderApi.update(formData).then(response => {
                    this.$message({
                        message: this.$t('common.optionSuccess'),
                        type: 'success'
                    })
                    this.fetchData()
                    this.formVisible = false
                })
            }else{
                taskOrderApi.add(formData).then(response => {
                    this.$message({
                        message: this.$t('common.optionSuccess'),
                        type: 'success'
                    })
                    this.fetchData()
                    this.formVisible = false
                })
            }
        } else {
          return false
        }
      })
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    editItem(record){
      this.selRow = record
      this.edit()
    },
    edit() {
      if (this.checkSel()) {
        this.isAdd = false
        let form = Object.assign({}, this.selRow)
        this.form = form
        this.formTitle = '编辑接单订单类'
        this.formVisible = true

        if(this.$refs['form'] !== undefined) {
          this.$refs['form'].resetFields()
        }
        //如果表单初始化有特殊处理需求,可以在resetForm中处理
              }
    },
    handle(type,record){
      if(type == 1){
        this.formTitle = '查看申述'
      }else {
        this.formTitle = '处理申述'
      }
      this.selRow = record
      this.formVisible = true
      proofApi.getProofByOrderId(record.id).then(res =>{
        this.taskPoofs = res.data
      })
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
            taskOrderApi.remove(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          }).catch( err=> {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    }

  }
}
